cmake_minimum_required(VERSION 3.16)
project(SeanNetwork)

# 设置C++标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 添加ASIO头文件路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/asio/include)

# 添加项目头文件路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/SeanNetworkLib)

# 设置源文件目录
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/SeanNetworkLib)

# 设置源文件列表
set(SOURCE_FILES
    ${SOURCE_DIR}/KcpChannel.cpp
    ${SOURCE_DIR}/TcpChannel.cpp
    ${SOURCE_DIR}/NetworkMessage.cpp
    ${SOURCE_DIR}/MessagePool.cpp
    ${SOURCE_DIR}/NetworkManager.cpp
    ${SOURCE_DIR}/NetworkClient.cpp
    ${SOURCE_DIR}/NetworkServer.cpp
    ${SOURCE_DIR}/ClientSession.cpp
    ${SOURCE_DIR}/Serializer.cpp
    ${SOURCE_DIR}/TimerManager.cpp
    ${SOURCE_DIR}/UdpSocket.cpp
    ${SOURCE_DIR}/thirdpart/kcp/ikcp.c
)

# 设置头文件列表
set(HEADER_FILES
    ${SOURCE_DIR}/KcpChannel.h
    ${SOURCE_DIR}/TcpChannel.h
    ${SOURCE_DIR}/INetworkChannel.h
    ${SOURCE_DIR}/NetworkMessage.h
    ${SOURCE_DIR}/MessagePool.h
    ${SOURCE_DIR}/NetworkManager.h
    ${SOURCE_DIR}/NetworkClient.h
    ${SOURCE_DIR}/NetworkServer.h
    ${SOURCE_DIR}/ClientSession.h
    ${SOURCE_DIR}/Serializer.h
    ${SOURCE_DIR}/TimerManager.h
    ${SOURCE_DIR}/UdpSocket.h
    ${SOURCE_DIR}/SeanNetwork.h
    ${SOURCE_DIR}/thirdpart/kcp/ikcp.h
)

# 创建库
add_library(SeanNetwork STATIC ${SOURCE_FILES} ${HEADER_FILES})

# 平台相关设置
if(WIN32)
    target_compile_definitions(SeanNetwork PRIVATE 
        _WIN32_WINNT=0x0601 
        _SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING
        _SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING
        ASIO_STANDALONE
        ASIO_HAS_STD_ADDRESSOF
        ASIO_HAS_STD_ARRAY
        ASIO_HAS_CSTDINT
        ASIO_HAS_STD_SHARED_PTR
        ASIO_HAS_STD_TYPE_TRAITS
    )
    target_link_libraries(SeanNetwork PRIVATE ws2_32 mswsock)
else()
    target_compile_definitions(SeanNetwork PRIVATE 
        ASIO_STANDALONE
    )
    # 对于非Windows平台，可能需要链接pthread
    find_package(Threads REQUIRED)
    target_link_libraries(SeanNetwork PRIVATE Threads::Threads)
endif()

# 设置输出路径
set_target_properties(SeanNetwork PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

# 设置包含目录
target_include_directories(SeanNetwork PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/SeanNetworkLib>
    $<INSTALL_INTERFACE:include/SeanNetwork>
)

# 安装配置
install(TARGETS SeanNetwork
    EXPORT SeanNetworkTargets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION include
)

install(FILES
    ${HEADER_FILES}
    DESTINATION include/SeanNetwork
)

install(EXPORT SeanNetworkTargets
    FILE SeanNetworkTargets.cmake
    NAMESPACE SeanNetwork::
    DESTINATION lib/cmake/SeanNetwork
)

# 如果有示例代码，添加示例子目录
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/examples" AND IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/examples")
    add_subdirectory(examples)
endif()